package com.sdsmdg.harjot.vectormaster.models;

import ohos.agp.render.BlendMode;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.RectFloat;

import com.sdsmdg.harjot.vectormaster.utilities.parser.PathParser;

/**
 * Model class to keep clip path details
 */
public class ClipPathModel {
    private String name;
    private String pathData;
    private Path originalPath;
    private Path path;
    private Paint clipPaint;

    public ClipPathModel() {
        path = new Path();
        clipPaint = new Paint();
        clipPaint.setAntiAlias(true);
        clipPaint.setBlendMode(BlendMode.CLEAR);
    }

    /**
     * Build the Path object from path string
     */
    public void buildPath() {
        originalPath = PathParser.createPathFromPathData(pathData);
        path = new Path(originalPath);
    }

    /**
     * Updated the Matrix details to path
     *
     * @param matrix Matrix
     */
    public void transform(Matrix matrix) {
        path = new Path(originalPath);
        path.transform(matrix);
    }

    public Paint getClipPaint() {
        return clipPaint;
    }

    public void setClipPaint(Paint clipPaint) {
        this.clipPaint = clipPaint;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPathData() {
        return pathData;
    }

    public void setPathData(String pathData) {
        this.pathData = pathData;
    }

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

    /**
     * Create the Path from offset and scale param
     *
     * @param offsetX  offsetX
     * @param offsetY  offsetY
     * @param scaleX   scaleX
     * @param scaleY   scaleX
     * @return Vector image Path
     */
    public Path getScaledAndOffsetPath(float offsetX, float offsetY, float scaleX, float scaleY) {
        Path newPath = new Path(path);
        newPath.offset(offsetX, offsetY);
        newPath.transform(getScaleMatrix(newPath, scaleX, scaleY));
        return newPath;
    }

    /**
     * Create scale matrix
     *
     * @param srcPath srcPath
     * @param scaleX  scaleX
     * @param scaleY  scaleY
     * @return scaleMatrix
     */
    public Matrix getScaleMatrix(Path srcPath, float scaleX, float scaleY) {
        Matrix scaleMatrix = new Matrix();
        RectFloat rectF = new RectFloat();
        srcPath.computeBounds(rectF);
        scaleMatrix.setScale(scaleX, scaleY, rectF.left, rectF.top);
        return scaleMatrix;
    }

}
